package com.jiaoyou.baojian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.adapter.UserAdapter;
import com.jiaoyou.baojian.entity.UserInfo;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback2;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;

public class FansActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private List<UserInfo> list;
    private UserAdapter adapter;

    private long userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle("粉丝");
        ButterKnife.bind(this);

        userId = getIntent().getIntExtra("userid", 0);

        list = new ArrayList<>();
        adapter = new UserAdapter();
        LinearLayoutManager giftLinearLayoutManager = new LinearLayoutManager(this);
        giftLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(giftLinearLayoutManager);
        recyclerView.setAdapter(adapter);
        adapter.setNewData(list);
        adapter.setEmptyView(getEmptyView());
        adapter.notifyDataSetChanged();
        adapter.setOnItemClickListener(this);

        getData();
    }

    private void getData() {
        Api.getInstance().getFansList(userId, new BaofangCallback2<UserInfo>() {
            @Override
            public void onFailure(Call call, Throwable t) {
                toast("获取数据错误:" + t.getLocalizedMessage());
                finish();
            }

            @Override
            public void onSuccess(List<UserInfo> response) {
                list.addAll(response);
                adapter.notifyDataSetChanged();
            }
        });
    }

    private View getEmptyView() {
        TextView textView = new TextView(this);
        textView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        textView.setGravity(Gravity.CENTER);
        textView.setText("没有数据");
        return textView;
    }

    @Override
    public int getContentView() {
        return R.layout.activity_follow;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        UserInfo userInfo = list.get(position);
        Intent intent = new Intent(this, UserInfoActivity.class);
        intent.putExtra("userinfo", userInfo);
        startActivity(intent);
    }
}
